package club.pchome.util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import club.pchome.dto.Result;
import club.pchome.ex.ServiceException;


/**
 * 非空校验工具类
 */
public class TUtil {

	private static String MSG = "表单信息不完整";

	/** 图片类型 */
	public static final String[] IMAGETYPES = { "image/png", "image/gif", "image/jpeg", "image/jpg", "image/x-icon" };
	/** app应用类型 */
	public static final String[] APPTYPES = { "application/octet-stream", "application/vnd.android.package-archive" };

	/** app应用类型 */
	public static final String[] VIDEOTYPES = { "video/mp4" };

	/**
	 * 判断是否有该类型
	 * 
	 * @param regex ,指定类型
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static boolean ishaveType(String[] regex, String type) throws Exception {

		boolean flag = false;
		Set<String> set1 = new HashSet<String>(Arrays.asList(regex));
		if (set1.contains(type)) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 判断是否有该类型
	 * 
	 * @param regex ,指定类型
	 * @param type
	 * @return
	 * @throws Exception
	 */
	public static boolean ishaveType(Integer[] regex, Integer type) throws Exception {

		boolean flag = false;
		Set<Integer> set1 = new HashSet<Integer>(Arrays.asList(regex));
		if (set1.contains(type)) {
			flag = true;
		}
		return flag;
	}

	/**
	 * 验证指定对象是否为空
	 * 
	 * @param msg 返回的信息（若为null则返回默认信息：表单元素）
	 * @param obj 待验证对象
	 * @throws Exception
	 */
	public static void checkObj(String msg, Object obj) throws Exception {

		msg = StringUtils.isEmpty(msg) ? MSG : msg;
		if (obj instanceof String) {
			checkStr(msg, (String) obj);
		} else if (null == obj) {
			throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, msg));
		}
	}

	/**
	 * <h5>示例：checkObj(new
	 * String[]{"消息1","消息2","消息3","消息4"},"str1","str2","",null);</h5>
	 * 如示例所示：测试{"str1","str2","",null}这个数组，若该数组的某一个为null/"" 则以它的索引取出 new
	 * String[]{"消息1","消息2","消息3","消息4"}中的提示信息
	 * 
	 * @param msgs 返回的信息（若为null则返回默认信息：表单元素）
	 * @param      obj... 待验证对象（可变参数）
	 * @throws Exception
	 */
	public static void checkObj(String[] msgs, Object... obj) throws Exception {

		if (obj == null || obj.length <= 0) {
			throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, MSG));
		}
		int oL = obj.length;
		if (msgs != null) {
			int mL = msgs.length;
			if (mL == oL) {
				for (int i = 0; i < oL; i++) {
					String msg = StringUtils.isEmpty(msgs[i]) ? MSG : msgs[i];
					if (obj[i] instanceof String) {
						checkStr(msg, (String) obj[i]);
					} else if (null == obj[i]) {
						throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, msg));
					}
				}
			} else if (mL < oL) {
				// 消息小于对象数，则需要构建默认消息
				for (int i = 0; i < oL; i++) {
					String msg = MSG;
					if (i < mL) {
						msg = StringUtils.isEmpty(msgs[i]) ? MSG : msgs[i];
					}
					if (obj[i] instanceof String) {

						checkStr(msg, (String) obj[i]);
					} else if (null == obj[i]) {
						throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, msg));
					}
				}
			}
		} else {
			for (int i = 0; i < oL; i++) {
				if (obj[i] instanceof String) {
					checkStr(MSG, (String) obj[i]);
				} else if (null == obj[i]) {
					throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, MSG));
				}
			}
		}
	}

	/**
	 * 验证指定字符串是否为空
	 * 
	 * @param msg 返回的信息（若为null则返回默认信息：表单元素）
	 * @param str 待验证字符串
	 * @throws Exception
	 */
	public static void checkStr(String msg, String str) throws Exception {
		msg = StringUtils.isEmpty(msg) ? MSG : msg;
		if (!StringUtils.isNotBlank(str)) {
			throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, msg));
		}
	}

	/**
	 * <h5>示例：checkStr(new
	 * String[]{"消息1","消息2","消息3","消息4"},"str1","str2","",null);</h5>
	 * 如示例所示：测试{"str1","str2","",null}这个数组，若该数组的某一个为null/"" 则以它的索引取出 new
	 * String[]{"消息1","消息2","消息3","消息4"}中的提示信息
	 * 
	 * @param msgs 返回的信息数组,数组长度请与可变参数长度一致（若不一致，则会设置默认提示信息）
	 * @param strs 待验证对象（可变参数）
	 * @throws Exception
	 */
	public static void checkStr(String[] msgs, String... strs) throws Exception {
		if (strs == null || strs.length <= 0) {
			throw new NullPointerException(MSG);
		}
		int sL = strs.length;
		if (msgs != null) {
			int mL = msgs.length;
			if (mL == sL) {
				for (int i = 0; i < sL; i++) {
					if (StringUtils.isEmpty(strs[i])) {
						String msg = StringUtils.isEmpty(msgs[i]) ? MSG : msgs[i];
						throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, msg));
					}
				}
			} else if (mL < sL) {
				for (int i = 0; i < sL; i++) {
					if (StringUtils.isEmpty(strs[i])) {
						String msg = MSG;
						if (i < mL) {
							msg = StringUtils.isEmpty(msgs[i]) ? MSG : msg;
						}
						throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, msg));
					}
				}
			}
		} else {
			for (int i = 0; i < sL; i++) {
				if (StringUtils.isEmpty(strs[i])) {
					throw new ServiceException(Result.initFailureResult(Result.ERROR_CORD, MSG));
				}
			}
		}
	}

}
